//
//  LeaveMessageViewController.m
//  onlineCustoSDK
//
//  Created by 曹宗华 on 2018/7/17.
//  Copyright © 2018年 com.netinfosz. All rights reserved.
//

#import "LeaveMessageViewController.h"

@interface LeaveMessageViewController ()<UITextFieldDelegate,UITextViewDelegate>

@end

@implementation LeaveMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"留言";
    self.titleTextfield.hidden=NO;
    self.contentTextView.hidden=NO;
    self.nameKeyLabel.hidden=NO;
    self.nameTextField.hidden=NO;
    self.phonekeyLabel.hidden=NO;
    self.phoneTextField.hidden=NO;
    self.mailKeyLabel.hidden=NO;
    self.mailTextField.hidden=NO;
    self.commitBtn.hidden=NO;
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:self.closeBtn];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    UITapGestureRecognizer *gesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.view.userInteractionEnabled=YES;
    [self.view addGestureRecognizer:gesture];
    // Do any additional setup after loading the view.
}
- (void)tapAction:(UITapGestureRecognizer*)gesture
{
    [self.view endEditing:YES];
}
- (void)keyBoardWillShow:(NSNotification*)sender
{
    NSLog(@"键盘即将弹出");
     //键盘最后的frame
    CGRect keyBoardRect=[(sender.userInfo[UIKeyboardFrameEndUserInfoKey]) CGRectValue];
    CGFloat height=keyBoardRect.size.height;
    NSLog(@"height:%f",height);
    if(self.titleTextfield.isFirstResponder)
    {
        if ((self.titleTextfield.y+self.titleTextfield.height)>(APP_Frame_Height-height))
        {
            self.view.y=-height;
        }
    }
    if (self.phoneTextField.isFirstResponder) {
        if ((self.phoneTextField.y+self.phoneTextField.height)>(APP_Frame_Height-height))
        {
            self.view.y=-height;
        }
        
    }
    if(self.mailTextField.isFirstResponder)
    {
        if ((self.mailTextField.y+self.mailTextField.height)>(APP_Frame_Height-height))
        {
            self.view.y=-height;
        }
    }
    if(self.nameTextField.isFirstResponder)
    {
        if ((self.nameTextField.y+self.nameTextField.height)>(APP_Frame_Height-height))
        {
            self.view.y=-height;
        }
    }
    if(self.contentTextView.isFirstResponder)
    {
        if ((self.contentTextView.y+self.contentTextView.height)>(APP_Frame_Height-height))
        {
            self.view.y=-height;
        }
    }
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
//    [self.navigationController setToolbarHidden:YES];
}
- (void)keyBoardWillHide:(NSNotification*)sender
{
     NSLog(@"键盘即将隐藏");
    self.view.y=0;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UIButton*)closeBtn
{
    if(!_closeBtn)
    {
        _closeBtn=[[UIButton alloc] init];
        _closeBtn.frame=CGRectMake(0, 0, 30, 30);
        [_closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
        [_closeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_closeBtn addTarget:self
                      action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}
- (void)close
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (UITextField*)titleTextfield
{
    if(!_titleTextfield)
    {
         _titleTextfield=[[UITextField alloc] initWithFrame:CGRectMake(30, kNaviBarHeight+20, App_Frame_Width-60, 35)];
        _titleTextfield.delegate=self;
        _titleTextfield.borderStyle=UITextBorderStyleRoundedRect;
        _titleTextfield.placeholder=@"请输入标题";
        _titleTextfield.textAlignment=NSTextAlignmentLeft;
        [self.view addSubview:_titleTextfield];
    }
    return _titleTextfield;
    
}
- (UITextView*)contentTextView
{
    if(!_contentTextView)
    {
        _contentTextView=[[UITextView alloc] initWithFrame:CGRectMake(30, self.titleTextfield.y+self.titleTextfield.height+50, App_Frame_Width-60, 100)];
        _contentTextView.delegate=self;
        _contentTextView.backgroundColor=[UIColor grayColor];
        [self.view addSubview:_contentTextView];
    }
    return _contentTextView;
}
- (UILabel*)nameKeyLabel
{
    if(!_nameKeyLabel)
    {
        _nameKeyLabel=[[UILabel alloc] initWithFrame:CGRectMake(30, self.contentTextView.y+self.contentTextView.height+30, 50, 30)];
        _nameKeyLabel.text=@"姓名";
        [self.view addSubview:_nameKeyLabel];
    }
    return _nameKeyLabel;
}
- (UITextField*)nameTextField
{
    if (!_nameTextField) {
        _nameTextField=[[UITextField alloc] initWithFrame:CGRectMake(self.nameKeyLabel.x+self.nameKeyLabel.width+20, self.nameKeyLabel.y, App_Frame_Width*0.60, 30)];
        _nameTextField.delegate=self;
        _nameTextField.borderStyle=UITextBorderStyleRoundedRect;
        _nameTextField.textAlignment=NSTextAlignmentLeft;
        _nameTextField.placeholder=@"请输入姓名";
        [self.view addSubview:_nameTextField];
    }
    return _nameTextField;
}
- (UILabel*)phonekeyLabel
{
    if (!_phonekeyLabel)
    {
        _phonekeyLabel=[[UILabel alloc] initWithFrame:CGRectMake(30, self.nameKeyLabel.y+self.nameKeyLabel.height+20, 50, 30)];
        _phonekeyLabel.text=@"电话";
        [self.view addSubview:_phonekeyLabel];
    }
    return _phonekeyLabel;
}
- (UITextField*)phoneTextField
{
    if(!_phoneTextField)
    {
        _phoneTextField=[[UITextField alloc] initWithFrame:CGRectMake(self.phonekeyLabel.x+self.phonekeyLabel.width+10, self.phonekeyLabel.y, App_Frame_Width*0.60, 30)];
        _phoneTextField.delegate=self;
        _phoneTextField.borderStyle=UITextBorderStyleRoundedRect;
        _phoneTextField.textAlignment=NSTextAlignmentLeft;
        _phoneTextField.placeholder=@"请输入手机号码";
        [self.view addSubview:_phoneTextField];
    }
    return _phoneTextField;
}
- (UILabel*)mailKeyLabel
{
    if(!_mailKeyLabel)
    {
        _mailKeyLabel=[[UILabel alloc] initWithFrame:CGRectMake(30, self.phonekeyLabel.y+self.phonekeyLabel.height+20, 50, 30)];
        _mailKeyLabel.text=@"邮箱";
        [self.view addSubview:_mailKeyLabel];
    }
    return _mailKeyLabel;
}
-  (UITextField*)mailTextField
{
    if(!_mailTextField)
    {
        _mailTextField=[[UITextField alloc] initWithFrame:CGRectMake(self.mailKeyLabel.x+self.mailKeyLabel.width+20, self.mailKeyLabel.y, App_Frame_Width*0.60, 30)];
        _mailTextField.delegate=self;
        _mailTextField.borderStyle=UITextBorderStyleRoundedRect;
        _mailTextField.textAlignment=NSTextAlignmentLeft;
        _mailTextField.placeholder=@"请输入邮箱地址";
        [self.view addSubview:_mailTextField];
        
    }
    return _mailTextField;
}
- (UIButton*)commitBtn
{
    if(!_commitBtn)
    {
        _commitBtn=[[UIButton alloc] initWithFrame:CGRectMake((App_Frame_Width-80)/2, self.mailKeyLabel.y+self.mailKeyLabel.height+20, 80, 40)];
        [_commitBtn setTitle:@"提交留言" forState:UIControlStateNormal];
        [_commitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _commitBtn.backgroundColor=[UIColor grayColor];
        [_commitBtn addTarget:self action:@selector(commitLeaveMessage) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_commitBtn];
        
        
    }
    return _commitBtn;
}
- (void)commitLeaveMessage
{
    NSLog(@"提交了留言");
    NSDictionary *message=nil;
    if(self.messageTitle!=nil&&self.leftMessage!=nil&&self.name!=nil&&self.mailAddress!=nil&&self.phoneNum!=nil)
    {
  message=@{@"subject":self.messageTitle,@"content":self.leftMessage,@"attachment":@"",@"channelId":@"",@"name":self.name,@"email":self.mailAddress,@"phone":self.phoneNum};
    }else
    {
        [CCDemoUtil showAlertWithTitle:@"提示" content:@"输入内容不能为空"];
    }
    if(message==nil)
    {
        return;
    }
    [[YiChatClient shareInstance] addLeftMessage:message Success:^(NSString *info) {
        NSLog(@"留言成功:%@",info);
        NSData *data=[info dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *returnDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        if([returnDic[@"resultCode"] intValue]==0)
        {
            [CCDemoUtil showAlertWithTitle:@"提示" content:@"留言成功"];
        }
        
    } fail:^(NSError *error) {
        NSLog(@"留言失败:%@",error.localizedDescription);
         [CCDemoUtil showAlertWithTitle:@"提示" content:error.localizedDescription];
    }];
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
  
    if(textField==self.titleTextfield)
    {
        self.messageTitle=textField.text;
    }else if (textField==self.nameTextField)
    {
        self.name=textField.text;
    }else if (textField==self.phoneTextField)
    {
        self.phoneNum=textField.text;
    }else if (textField==self.mailTextField)
    {
        self.mailAddress=textField.text;
    }
      NSLog(@"输入结束...");
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"%@", textField.text);//文本变化内容
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    NSLog(@"clear button被点击了...");
    return YES;
}

//点击return键时调用的方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"return键点击了...");
    [textField resignFirstResponder];//辞去第一响应
    if(textField==self.titleTextfield)
    {
        self.messageTitle=textField.text;
    }else if (textField==self.nameTextField)
    {
        self.name=textField.text;
    }else if (textField==self.phoneTextField)
    {
        self.phoneNum=textField.text;
    }else if (textField==self.mailTextField)
    {
        self.mailAddress=textField.text;
    }
    NSLog(@"用户信息:%@,%@,%@,%@",self.name,self.phoneNum,self.mailAddress,self.messageTitle);
    return YES;
}
//文本视图委托代理
#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    NSLog(@"即将开始输入...");
    return YES;//返回YES允许输入，NO禁止输入
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    NSLog(@"即将结束输入...");
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    NSLog(@"已经开始输入了...");
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSLog(@"已经结束输入了...");
     self.leftMessage=textView.text;
    NSLog(@"留言结束:%@",self.leftMessage);
}

- (void)textViewDidChange:(UITextView *)textView
{
    NSString *str = textView.text;//文本内容变化时调用
    NSLog(@"%@", str);
}

//点击return键或delete键时调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //如果点击了return键，则收起键盘
    if ([text isEqualToString:@"\n"])
    {
        NSLog(@"return键点击了");
        self.leftMessage=textView.text;
        NSLog(@"留言:%@",self.leftMessage);
        [textView resignFirstResponder];//辞去第一响应
    }
    
    //如果点击了delete键
    if ([text isEqualToString:@""])
    {
        NSLog(@"delete键点击了");
    }
    
    return YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
